import { Eventable } from "./Eventable.js";

const LEVELS = [0, 100, 300, 600, 1000];

export class InfoPanel extends Eventable {
    #score: JQuery;
    #speed: JQuery;

    constructor(score: JQuery | string, speed: JQuery | string) {
        super();
        this.#score = $(score as any);
        this.#speed = $(speed as any);
    }

    get score(): number {
        return ~~this.#score.text() || 0;
    }

    set score(value: number) {
        this.#score.text(value || 0);
    }

    get speed(): number {
        return ~~this.#speed.val()! || 1;
    }

    set speed(value: number) {
        this.#speed.val(value || 1);
    }

    get interval(): number {
        return 1100 - this.speed * 100;
    }

    sppedUp(): void {
        const speed = this.speed + 1;
        if (speed > 9) {
            return;
        }
        this.speed = speed;
        this.trigger("speedUp", {
            speed: speed,
            interval: this.interval
        });
    }

    addByLevel(level: number): void {
        this.addScore(LEVELS[level]);
    }

    addScore(score: number): void {
        const LEVEL_STEP = 10000;
        const oldScoreLevel = ~~(this.score / LEVEL_STEP);
        this.score = score + this.score;
        const newScoreLevel = ~~(this.score / LEVEL_STEP);
        if (newScoreLevel > oldScoreLevel) {
            this.sppedUp();
        }
    }
}
